<?php

class Fengqiaoju_Controller_Action extends Zend_Controller_Action {

	//初始化
	public function init() {

		//读取配置文件
		$config = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/application.ini' );
		$this->view->config = $this->config = $config->toArray ();
		
		$modelName = $this->_request->getModuleName ();
		$modelName1 = ucfirst ( $modelName );

		//自动加载model
		$autoLoader = new Zend_Loader_Autoloader_Resource ( array ( 
			'basePath' => APPLICATION_PATH , 
			'namespace' => '' , 
			'resourceTypes' => array ( 
			'Group' => array ( 
				'path' => 'modules/' . $modelName . '/models' , 
				'namespace' => $modelName1 . '_Models_' 
			) , 
			'User' => array ( 
				'path' => 'modules/user/models' , 
				'namespace' => 'User_Models_' 
		) 
		) 
		) );
		$this->view->headScript = '<script type="text/javascript" src="' . $this->config ['url'] ['source'] . '/js/jquery.min.js"></script>
<script type="text/javascript" src="' . $this->config ['url'] ['source'] . '/js/v3.js?' . $this->config ['version'] ['main'] . '"></script>';
		$this->view->headStyle = '<link type="text/css" rel="stylesheet" href="' . $this->config ['url'] ['source'] . '/theme/default/public/style.css?' . $this->config ['version'] ['main'] . $this->config ['version'] ['css'] . '" />';
		//head
		if ($modelName) {
			$this->view->headStyle .= '<link type="text/css" rel="stylesheet" href="' . $this->config ['url'] ['source'] . '/theme/default/' . $modelName . '/style.css?' . $this->config ['version'] ['main'] . $this->config ['version'] ['css'] . '" />';
			$this->view->headScript .= '<script type="text/javascript" src="' . $this->config ['url'] ['source'] . '/js/' . $modelName . '.js?' . $this->config ['version'] ['main'] . $this->config ['version'] ['js'] . '"></script>';
		}
		
		$this->userObject = new User_Models_User ();
		$this->view->topBar = $this->topBar ();
	}

	//头部菜单
	protected function topBar() {

		if ($this->userObject->getUserId ()) {
			$msg = $this->userObject->getMewMsg ();
			if($this->userObject->getUserId()<4){
				$newMsg = "<a href='".$this->config['url']['admin']."' class=red title='管理后台'>管理</a>";
			}
			if ($msg ['newpm']) {
				$newMsg = "<a href=''.$this->config['url']['user'].'/profile/setting/setup/message' class=red title='新消息'><img src='/source/images/voice.gif' border=0 /></a>";
			}
			$topbar = $newMsg . '
	    	<a href="' . $this->config ['url'] ['user'] . '/index/logout" class="notouming">退出</a>
	    	<a href="' . $this->config ['url'] ['user'] . '/profile/setting/setup/avatar" class="notouming">' . $this->userObject->getUserName () . '</a>
	    	<a href="' . $this->config ['url'] ['user'] . '/" class="notouming">个人中心</a>
	        ';
		}
		else {
			$topbar = '<a href="' . $this->config ['url'] ['user'] . '/index/register" class="notouming">注册</a><span class="vline"></span>
	        <a href="' . $this->config ['url'] ['user'] . '/index/login" class="notouming">登入</a><span class="vline"></span>';
		}
		
		return $topbar;
	}

	//判断是否登入
	protected function isLogin($url = '') {

		if ($uid = $this->userObject->getUserId ()) {
			if ($url) {
				$this->_redirect ( $url );
			}
			else {
				return $uid;
			}
		}
		else {
			return false;
		}
	}

	//产生验证码
	protected function yzm() {

		$captcha = new Zend_Captcha_Image ( array ( 
			'font' => 'source/images/msyhbd.ttf' , //字体文件路径
'fontsize' => 12 , //字号
'imgdir' => APPLICATION_PATH . '/../public/source/_yzm' , //验证码图片存放位置
'session' => $this->codeSession , //验证码session值
'width' => 90 , //图片宽
'height' => 28 , //图片高
'wordlen' => 4 , //字母数
'imgurl' => $this->view->url ['site'] ['source'] . '/source/_yzm/' , 'lineNoiseLevel' => 1 , 'dotNoiseLevel' => 4 
		) );
		$captcha->setGcFreq ( 5 ); //设置删除生成的旧的验证码图片的随机几率
		$captcha->generate (); //生成图片
		

		$yzm ['url'] = $this->view->url ['site'] ['source'] . '/source/_yzm/' . $captcha->getId () . '.png';
		$yzm ['code'] = $captcha->getWord ();
		$this->codeSession->code = $yzm ['code'];
		return $yzm;
	}

	//生成目录
	protected function mkdirs($dir , $mode = '0777') {

		if (! is_dir ( $dir )) {
			$this->mkdirs ( dirname ( $dir ), $mode );
			mkdir ( $dir, $mode );
		}
		@chmod ( $dir, 0777 );
		return true;
	}

	//判断图片是否存在
	protected function viewImage($url) {

		$head = get_headers ( $url );
		foreach ( $head as $val ) {
			$arr = explode ( 'Content-Type: ', $val );
			if ($arr [1]) {
				$arr1 = explode ( '/', $arr [1] );
				if ($arr1 [0] == 'image') {
					return 1;
				}
			}
		}
		return 0;
	}

	//信息提示
	public function message($msg , $goUrl = 'javascript:history.go(-1)' , $type = 'error') {

		$layout = new Zend_Layout ();
		$layout->setLayoutPath ( APPLICATION_PATH . '/layouts/scripts' );
		$layout->setLayout ( 'message' );
		$this->view->title = '信息提示 ' . $type;
		$this->view->type = $type;
		$this->view->goUrl = $goUrl;
		$layout->content = $msg;
		echo $layout->render ();
		exit ();
	
	}

	/**
	 * 
	 * 操作成功提示
	 */
	protected function ok($msg , $goUrl = 'javascript:history.go(-2)') {

		$this->message ( $msg, $goUrl, 'ok' );
	}

	protected function upload() {

		$info = array ();
		$savePath = APPLICATION_PATH . '/../public' . $this->config ['upload'] ['dir'] . "/" . date ( 'Y' ) . "/" . date ( 'm' ) . "/" . date ( 'd' );
		$this->mkdirs ( $savePath );
		$adapter = new Zend_File_Transfer_Adapter_Http ();
		$adapter->setDestination ( $savePath );
		//上传配置
		$adapter->addValidator ( 'Extension', false, $this->config ['upload'] ['extension'] )->addValidator ( 'Size', false, array ( 
			'min' => $this->config ['upload'] ['minsize'] , 'max' => $this->config ['upload'] ['maxsize'] 
		) )->addValidator ( 'Count', false, array ( 
			'min' => intval ( $this->config ['upload'] ['mincount'] ) , 'max' => intval ( $this->config ['upload'] ['maxcount'] ) 
		) ); //上传文件数量
		//获取基本信息
		$fileInfo = $adapter->getFileInfo ();
		
		//重命名配置
		if ($fileInfo) {
			foreach ( $fileInfo as $k => $v ) {
				$fname = $v ['name'];
				$exts = @split ( "[/\\.]", $fname );
				$n = count ( $exts ) - 1;
				$extName = $exts [$n];
				$info ['filename'] = $exts [0];
				
				$filename = md5 ( $info ['filename'] . time () ) . '.' . $extName; //重命名
				$adapter->addFilter ( 'Rename', array ( 
					'target' => $filename , 'overwrite' => true 
				) ); //执行重命名
				

				//返回上传后出现在信息
				if (! $adapter->receive ()) {
					$messages = $adapter->getMessages (); //检测
					//Zend_Debug::dump($messages);
					$message = '';
					if (is_array ( $messages )) {
						foreach ( $messages as $k => $v ) {
							$message .= $k . ':' . $v . '<br>';
						}
					}
					else {
						$message = $messages;
					}
					$info ['message'] = $message;
				}
				else {
					$info ['filepath'] = $this->config ['upload'] ['dir'] . "/" . date ( 'Y' ) . "/" . date ( 'm' ) . "/" . date ( 'd' ) . "/" . $filename;
					$info ['message'] = '上传成功';
				}
			
			}
		}
		return $info;
	}

	//写日志
	protected function writeLog($log , $filename , $path = '') {

		if ($this->config ['site'] ['log']) {
			if ($path) {
				$path = APPLICATION_PATH . "/../Log/" . $path . "/" . date ( 'Y-m' );
			}
			else {
				$path = APPLICATION_PATH . "/../Log/" . date ( 'Y-m' );
			}
			$this->mkdirs ( $path );
			if ($filename) {
				$filename = $filename . '.log';
			}
			else {
				$filename = $path . date ( 'd' ) . '.log';
			}
			$file = $path . '/' . $filename;
			@$file = fopen ( $file, "a+" );
			@fwrite ( $file, $log );
			@fwrite ( $file, "\r\ndate:" . date ( 'Y-m-d H:i:s' ) . " \r\n" );
			@fclose ( $file );
		}
	}
}
?>